﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using diidon.util;
using System;
using System.Threading;
using System.Runtime.InteropServices;
using Beyond;
using Beyond.Ant;
using ZJ_WangSir;
using MyIdolUIFrameWork;

public class GameGoods
{
    public static Dictionary<int, GameGoods> goodsMap = new Dictionary<int, GameGoods>();
    public static void init()
    {
        new GameGoods(1, "去广告", 6800, "c27_code=001", 0);
        new GameGoods(2, "12个宝石", 600, "c27_code=002", 12);
        new GameGoods(3, "512个宝石", 12800, "c27_code=003", 512);
        new GameGoods(4, "1968个宝石", 32800, "c27_code=004", 1968);
        new GameGoods(5, "5184个宝石", 64800, "c27_code=005", 5184);
    }

    public static GameGoods GetGameGoods(int id)
    {
        if (goodsMap.ContainsKey(id))
        {
            return goodsMap[id];
        }
        return null;
    }

    public int id;
    public string name;
    public int price;
    public string purchaseCode;

    public int value;

    private GameGoods(int id, string name, int price, string purchaseCode, int value)
    {
        this.id = id;
        this.name = name;
        this.price = price;
        this.purchaseCode = purchaseCode;
        this.value = value;

        goodsMap.Add(id, this);
    }
}

public class PurchaseProxy
{
    public static void init()
    {
        GameGoods.init();
        Purchase.initialize("", new Purchase.CallbackDelegate(payCallback));
        //Purchase.setFree(true); //免费版本 便于测试 提交之前一定要注释!!!!!!!!!!!!!!!!!!!!!!!!
    }

    static void payCallback(int actionId, int goodsId, int locationId, string tradeOrderNumber, int result)
    {
        Debug.Log("My Purchase._CallbackDelegate " + actionId + ", " + goodsId + ", " + locationId + ", " + tradeOrderNumber + ", " + result);
        if (result < 1)
        {
            return;
        }
        GameGoods goods = GameGoods.GetGameGoods(goodsId);
        if (goods == null)
        {
            return;
        }


        if (goodsId == 1)
        {
            ZJ_WangSir.CUtils.SetRemoveAds(true);
            ZJ_WangSir.CUtils.CloseBannerAd();
            UIManager.Instance.SetVisible(UIName.UIRemoveAD, false);
            UIManager.Instance.SetVisible(UIName.UIAwardWord, true);
            if (MySetLanguage.GetLanguage().StartsWith("Chinese"))
            {
                if (MySetLanguage.GetLanguage() == "ChineseTraditional")
                    UIAwardWords.Instance.award_text.text = "您已成功去除廣告！！";
                else
                    UIAwardWords.Instance.award_text.text = "您已成功去除广告！！";
            }

            else
            {
                UIAwardWords.Instance.award_text.text = "You have successfully removed ads！！";
            }
            return;
        }

        else if (goodsId == 2)
        {
            CurrencyController.CreditBalance(goods.value);
            SelectLevelController.Instance.UpdateInfor();
            BELog.debug(" 添加刷新相关信息*-*-*-*-*");
        }
        else if (goodsId == 3)
        {
            CurrencyController.CreditBalance(goods.value);
            SelectLevelController.Instance.UpdateInfor();
        }

        else if (goodsId == 4)
        {
            CurrencyController.CreditBalance(goods.value);
            SelectLevelController.Instance.UpdateInfor();
        }
        else if (goodsId == 5)
        {
            CurrencyController.CreditBalance(goods.value);
            SelectLevelController.Instance.UpdateInfor();
        }

        UIManager.Instance.SetVisible(UIName.UIAwardWord, true);
        if (MySetLanguage.GetLanguage().StartsWith("Chinese"))
        {
            if (MySetLanguage.GetLanguage() == "ChineseTraditional")
                UIAwardWords.Instance.award_text.text = "恭喜獲得" + goods.value + "個寶石!";
            else
                UIAwardWords.Instance.award_text.text = "恭喜获得" + goods.value + "个宝石!";
        }

        else
        {
            UIAwardWords.Instance.award_text.text = "Congratulations to get  " + goods.value + " GEMS!";
        }
        
    }

    public static void pay(int goodsId)
    {
        GameGoods goods = GameGoods.GetGameGoods(goodsId);
        if (goods == null)
        {
            Debug.Log("GameGoods " + goodsId + " does not found");
            return;
        }
        Purchase.pay(BEAnt.getUserId(), goods.id, goods.name, goods.price, 1, goods.purchaseCode);
    }

    public static void restore(int goodsId)
    {
        GameGoods goods = GameGoods.GetGameGoods(goodsId);
        if (goods == null)
        {
            Debug.Log("GameGoods " + goodsId + " does not found");
            return;
        }
        Purchase.restore(BEAnt.getUserId(), goods.id, goods.name, goods.price, 1, goods.purchaseCode);
    }

}

